<template>
  <div>
    <!-- table -->
    <a-card :bordered="false">
      <a-row>
        <a-col :sm="6" :xs="24">
          <head-info title="$t('orderList.xd')" :content="stat[0]" :bordered="true"/>
        </a-col>
        <a-col :sm="6" :xs="24">
          <head-info title="$t('orderList.sh')" :content="stat[1]" :bordered="true"/>
        </a-col>
        <a-col :sm="6" :xs="24">
          <head-info title="$t('orderList.cg')" :content="stat[2]" :bordered="true"/>
        </a-col>
        <a-col :sm="6" :xs="24">
          <head-info title="$t('orderList.sb')" :content="stat[3]"/>
        </a-col>
      </a-row>
    </a-card>
    <a-card style="margin-top: 24px">
      <div class="table-operator">
        <div style="margin-bottom:15px;">
          {{ $t('orderList.lxsx') }}:
          <a-radio-group @change="handleChangeType" defaultValue="-1">
            <a-radio-button value="-1">{{ $t('orderList.qb') }}</a-radio-button>
            <a-radio-button value="1">{{ $t('orderList.dy') }}</a-radio-button>
            <a-radio-button value="2">{{ $t('orderList.hs') }}</a-radio-button>
            <a-radio-button value="3">{{ $t('orderList.ws') }}</a-radio-button>
            <a-radio-button value="4">{{ $t('orderList.ks') }}</a-radio-button>
          </a-radio-group>
        </div>
      </div>
      <a-table
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :loading="dataLoading"
      >
        <template slot="task_proto" slot-scope="text">
          <ul>
            <li>{{text.title}}</li>
            <li style="padding:10px 0;">
              <span v-if="text.type != 5">
                {{text.desc}}
              </span>
              <span v-else>
                <span>👀{{ $t('orderList.ck') }}</span>
              </span>
            </li>
            <li>💰{{text.single_reward}}</li>
            <li v-if="text.url"><a target="_blank" :href="text.url">{{ $t('orderList.lj') }}</a></li>
          </ul>
        </template>
        <template slot="user_proto" slot-scope="text">
          <ul>
            <li>{{text.username}}</li>
            <li v-if="text.nickname">{{text.nickname}}</li>
            <li v-if="text.headphoto"><img style="max-height:80px;max-width:80px;" :src="text.headphoto" alt=""></li>
          </ul>
        </template>
        <template slot="status" slot-scope="text">
          <template v-if="text==0 || text==1">
            <template>{{ $t('orderList.xd') }}</template>
          </template>
          <template v-else-if="text==2">
            <span style="color:blue">{{ $t('orderList.sh') }}</span>
          </template>
          <template v-else-if="text==3">
            <span style="color:green">{{ $t('orderList.cg') }}</span>
          </template>
          <template v-else>
            <span style="color:red">{{ $t('orderList.sb') }}</span>
          </template>
        </template>
        <template slot="imgs" slot-scope="text">
          <template>
            <img @click="handleClickReview($hostServer+text)" class="table-img" :src="$hostServer+text" alt="">
          </template>
        </template>
        <template slot="operation" slot-scope="text, record">
          <span>
            <a-popconfirm title="$t('orderList.sfysccx')" @confirm="handleClickDelete(record.id)">
              <a-button type="danger" size="small">{{ $t('orderList.sc') }}</a-button>
            </a-popconfirm>
          </span>
        </template>
      </a-table>
      <div style="margin-top:15px;">
        <MyPage :curr="page.curr" :size="page.size" :all="page.total" @changePage="handleChangePage" @changePageSize="handleChangePageSize"/>
      </div>
    </a-card>
    <DialogImg :img_url="curr_img_url" :is_show="curr_img_show"/>
  </div>
</template>

<script>
import HeadInfo from '@/components/tools/HeadInfo'
import { mixin, mixinDevice } from '@/utils/mixin'
import DialogImg from '../../components/DialogImg'
import MyPage from './Page'

export default {
  name: 'AdvancedForm',
  mixins: [mixin, mixinDevice],
  components: {
    HeadInfo,
    DialogImg,
    MyPage
  },
  data () {
    return {
      description: this.$t('orderList.bhdyhswsks'),
      loading: false,
      dataLoading: false,
      curr_img_url: '',
      curr_img_show: false,
      stat: [
        0, 0, 0, 0
      ],
      page: {
        curr: 1,
        size: 20,
        total: 0
      },
      // table
      columns: [
        {
          title: this.$t('orderList.lx'),
          dataIndex: 'task_type',
          key: 'task_type',
          scopedSlots: { customRender: 'task_type' }
        },
        {
          title: this.$t('orderList.rwxx'),
          dataIndex: 'task_proto',
          key: 'task_proto',
          scopedSlots: { customRender: 'task_proto' }
        },
        {
          title: this.$t('orderList.yhxx'),
          dataIndex: 'user_proto',
          key: 'user_proto',
          scopedSlots: { customRender: 'user_proto' }
        },
        {
          title: this.$t('orderList.tjnr'),
          dataIndex: 'content',
          key: 'content',
          scopedSlots: { customRender: 'content' }
        },
        {
          title: this.$t('orderList.tjtp'),
          dataIndex: 'imgs',
          key: 'imgs',
          scopedSlots: { customRender: 'imgs' }
        },
        {
          title: this.$t('orderList.hfnr'),
          dataIndex: 'reply',
          key: 'reply',
          scopedSlots: { customRender: 'reply' }
        },
        {
          title: this.$t('orderList.dqzs'),
          dataIndex: 'status',
          key: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: this.$t('orderList.cz'),
          key: 'action',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      data: [],
      errors: [],
      readyCnt: 0,
      runningCnt: 0,
      stopCnt: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList (type) {
      let url = '/order/list?page=' + this.page.curr + '&size=' + this.page.size
      if (type !== undefined && type !== '-1') {
        url += '&type=' + type
      }
      this.$http.get(url).then(res => {
        this.data = res.list
        this.page.total = res.total
        let status1 = 0
        let status2 = 0
        let status3 = 0
        let status4 = 0

        this.data.forEach(item => {
          switch (item.status) {
            case 0:
            case 1:
              status1++
              break
            case 2:
              status2++
              break
            case 3:
              status3++
              break
            default:
              status4++
          }
        })
        this.stat = [status1, status2, status3, status4]
      }).catch(err => {
        this.$notification.error({
          message: this.$t('orderList.sb'),
          description: err.message
        })
      })
    },
    handleClickRun (id) {
      // 2
      this.$http.post('/task/run', this.$qs.stringify({
        id: id
      })).then(res => {
        if (res.code === 0) {
          this.$notification.success({
            message: this.$t('orderList.cg'),
            description: res.msg
          })
          this.getList()
        } else {
          this.$notification.error({
            message: this.$t('orderList.sb'),
            description: res.msg
          })
        }
      }).catch(err => {
        this.$notification.error({
          message: this.$t('orderList.sb'),
          description: err.message
        })
      })
    },
    handleChangePageSize (size) {
      this.page.size = size
      this.getList()
    },
    handleChangePage (page) {
      this.page.curr = page
      this.getList()
    },
    handleClickReview (url) {
      this.curr_img_url = url
      this.curr_img_show = !this.curr_img_show
    },
    handleClickStop (id) {
      this.$http.post('/task/stop', this.$qs.stringify({
        id: id
      })).then(res => {
        if (res.code === 0) {
          this.$notification.success({
            message: this.$t('orderList.cg'),
            description: res.msg
          })
          this.getList()
        } else {
          this.$notification.error({
            message: this.$t('orderList.sb'),
            description: res.msg
          })
        }
      }).catch(err => {
        this.$notification.error({
          message: this.$t('orderList.sb'),
          description: err.message
        })
      })
    },
    handleClickDelete (id) {
      this.$http.post('/order/delete', this.$qs.stringify({
        id: id
      })).then(res => {
        const newData = this.data.filter(item => item.id !== id)
        this.data = newData
      }).catch(err => {
        this.$notification.error({
          message: '失败',
          description: err.message
        })
      })
    },
    handleSubmit (e) {
      e.preventDefault()
    },
    handleChangeType (e) {
      this.getList(e.target.value)
    }
  }
}
</script>

<style lang="less" scoped>
  .card{
    margin-bottom: 24px;
  }
</style>
